package com.m7788.util;

import java.lang.Thread.UncaughtExceptionHandler;

import android.app.Activity;
import android.content.Context;
import android.os.Process;
import android.os.SystemClock;
import android.util.Log;

import com.umeng.analytics.MobclickAgent;

import org.json.JSONException;
import org.json.JSONObject;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MyExceptionHanlder implements UncaughtExceptionHandler {

	private Context mContext = null;

	public MyExceptionHanlder(Context context) {
		this.mContext = context;
		// this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * java.lang.Thread.UncaughtExceptionHandler#uncaughtException(java.lang
	 * .Thread, java.lang.Throwable)
	 */
	@Override
	public void uncaughtException(Thread thread, Throwable ex) {
		if (ex != null) {
			Log.i("UncaughtException", ex.getMessage());
			StringBuilder sb = new StringBuilder();
			String temp = ex.getMessage();
			if (temp != null) {
				sb.append(temp);
			}
			sb.append("\r\n");
			sb.append(thread.getName());
			sb.append(" Trace: \r\n");
			StackTraceElement[] elements = ex.getStackTrace();
			if (elements != null) {
				for (StackTraceElement element : elements) {
					temp = element.toString();
					if (temp != null) {
						sb.append(temp);
					}
					sb.append("\r\n");
				}
			}

			// if the exception was thrown in a background thread inside
			// AsyncTask, then the actual exception can be found with getCause
			sb.append("Cause: ");
			Throwable theCause = ex.getCause();
			if (theCause != null) {
				temp = theCause.toString();
				if (temp != null) {
					sb.append(temp);
				}
			}
			sb.append("\r\nCause Stack:\r\n");
			theCause = ex.getCause();
			if (theCause != null) {
				elements = theCause.getStackTrace();
				if (elements != null) {
					for (StackTraceElement element : elements) {
						temp = element.toString();
						if (temp != null) {
							sb.append(temp);
						}
						sb.append("\r\n");
					}
				}
			}
			// you can write this content to file if necessary

//			Intent intent = new Intent(mContext, ReportPage.class);
//			intent.putExtra(ReportPage.REPORT_CONTENT,
//					sb.toString());
//			mContext.startActivity(intent);
			postExceptionLog(sb.toString());
			MobclickAgent.reportError(mContext,ex);
//			crash(mContext);

		}
	}

	private void postExceptionLog(final String errorMsg){
		final JSONObject jsonObject = new JSONObject();
		try {
			jsonObject.put("get_data",errorMsg);
			jsonObject.put("flag","2");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		new Thread(new Runnable() {
			@Override
			public void run() {
				Log.d("Chenguangxi",errorMsg+"test");
				HttpService.getInstance(mContext).postNetDiagnoseData(jsonObject);
			}
		}).start();
	}

	public void uncaughtException(Exception e) {
		uncaughtException(Thread.currentThread(), e);
	}

	private boolean crash(Context context) {
		if (context == null) {
			return false;
		}

		if (context instanceof Activity) {
			((Activity) context).finish();
		}

		Process.killProcess(Process.myPid());
		return true;
	}

}
